//@include "Controller.jsx"


// 创建对话框
var dialog = new Window("dialog", "保存选项");
checkBoxPanel = dialog.add("panel");
listGroup = dialog.add("group");
pathGroup = dialog.add("group");
buttonGroup = dialog.add("group");

buttonGroup.orientation = "row";
checkBoxPanelGroup1 = checkBoxPanel.add("group");
checkBoxPanelGroup2 = checkBoxPanel.add("group");

listGroup.add('statictext', undefined, '请选择一个水印:');

// 读取之前保存的选项索引
var savedIndex = loadIntFromFile();

// 创建一个下拉选框
var dropdown = listGroup.add('dropdownlist', undefined, [
    '定稿去水印(100)',
    '束凡水印(250)',
    '基本水印(100)'
]);

// 设置初始选项
if (savedIndex !== null && savedIndex >= 0 && savedIndex < dropdown.items.length) {
    dropdown.selection = savedIndex;
} else {
    dropdown.selection = 0; // 默认选择第一个选项
}

checkBoxPanelGroup3 = checkBoxPanel.add("group");

// 开始绘制 UI
createcheckBoxPanel(checkBoxPanel);
addPathSelector(pathGroup, "保存路径", updateSetting);

addButton(buttonGroup, "关闭", function() {
    dialog.close();
});

addButton(buttonGroup, "确认", function() {
    dialog.close();
    var selectedIndex = dropdown.selection ? dropdown.selection.index : 2;
    saveIntToFile(selectedIndex);
    saveProject(selectedIndex);
});

function addPathSelector(panel, textName, action) {
    var userSettings = loadDataFromFile();
    var nameLabel = panel.add("statictext", undefined, textName);
    var pathEditText = panel.add("edittext", undefined, decodeURI(userSettings.savePath));
    pathEditText.characters = 25; // 调整字符数以适应显示

    pathEditText.onChange = function() {
        action("savePath", encodeChineseToURL(pathEditText.text));
    };

    addButton(panel, "选择路径", function() {
        var selectedFolder = Folder.selectDialog("选择保存的文件夹");
        if (selectedFolder) {
            pathEditText.text = selectedFolder.fsName;
            action("savePath", encodeChineseToURL(pathEditText.text));
        }
    });
}

function addButton(panel, buttonName, action) {
    var button = panel.add("button", undefined, buttonName);
    button.onClick = function() {
        action();
    };
}

function createCheckBox(panel, key, checkBoxName) {
    var userSettings = loadDataFromFile();
    var checkBox = panel.add("checkbox", undefined, checkBoxName);
    checkBox.value = userSettings[key];

    checkBox.onClick = function() {
        updateSetting(key, checkBox.value);
    };
}

function createcheckBoxPanel(checkBoxPanel) {
    createCheckBox(checkBoxPanelGroup1, "isSavePSD", "PSD");
    createCheckBox(checkBoxPanelGroup1, "isSaveJPG", "JPG");
    createCheckBox(checkBoxPanelGroup1, "isSavePNG", "PNG");
    createCheckBox(checkBoxPanelGroup1, "isSaveJPGW", "JPG水印");
    createCheckBox(checkBoxPanelGroup1, "isSavePNGW", "PNG 水印");
    createCheckBox(checkBoxPanelGroup2, "isUseCurrDocName", "以文档名保存");
    createCheckBox(checkBoxPanelGroup2, "isSaveOwnFolder", "保存到格式文件夹");
    createCheckBox(checkBoxPanelGroup2, "isSaveDateFolder", "保存到时间文件夹");
    createCheckBox(checkBoxPanelGroup3, "isAlert", "保存后通知信息");
    createCheckBox(checkBoxPanelGroup3, "isCloseDocument", "保存后关闭文档");
    createCheckBox(checkBoxPanelGroup3, "isExecute", "保存后打开文件夹");
}

if (app.documents.length > 0) {
    dialog.show();
} else {
    alert("没有打开的文档");
}